#include"pfunction.h"
bool isfile(char* str)
{
     register int i;
     int lens;
     if(str==NULL)
          return false;
     lens=strlen(str);
     for(i=lens-1;i>0;i--)
       {
          if(*(str+i)=='.')
             {
                if(strcmp((str+i+1),suffix)==0)
                  {
                     return true;
                     }
                  else
                    {
                     return false;
                      }
               }
         }
      return false;
}
static char *fullpath;
void myfind(const char* pathname,r_list<string> *xfilename)
{
      int size;
      size=pathconf("/",_PC_PATH_MAX);
      size++;
      fullpath=(char*)malloc(size);
      if(fullpath==NULL)
          {
               throw new error_name("malloc for fullpath failed");
             }
      if(pathname==NULL)
          {
               throw new error_name("pathname is NULL");
           }
      strcpy(fullpath,pathname);
     myfindfile(xfilename);
     free(fullpath);
}
void myfindfile(r_list<string> *needfile)   //find file function
{
                DIR *sp;
                struct dirent *dirp;
                struct stat buf;
                char *ptr;
                string newline;
                        ptr=fullpath+strlen(fullpath);
                        *ptr++='/';
                        *ptr=0;
                sp=opendir(fullpath);
                if(sp!=NULL)
                    {
                      while((dirp=readdir(sp))!=NULL)
                           {
                             strcpy(ptr,dirp->d_name);
                             if(lstat(fullpath,&buf)<0)
                                  {
                                    continue;
                                           }
                                  if(S_ISDIR(buf.st_mode))
                                    {
                     if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
                                             {
                                               continue;
                                              }
                                            myfindfile(needfile);
                                    }
                                  if(S_ISREG(buf.st_mode))
                                         {
 				if(isfile(dirp->d_name))
                                              {
                                           newline=fullpath;
                                           needfile->creater_list(newline);
                                              }
                                          }
                                       else
                                           {
                                              continue;
                                                 }
                             }
                         }
                if(closedir(sp)<0)
                  {
                      throw new error_name("close dir failed");
                  }
}
